<template>
	<div>
		<div class="search">
			<el-form ref="searchForm" :inline="true" :model="searchForm" class="form-inline" size="mini">
				<el-form-item label="名称：" prop="name">
					<el-input clearable v-model.trim="searchForm.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="执行状态：" prop="work_order_type_id">
					<el-select clearable v-model="searchForm.exe_status" filterable placeholder="请选择">
						<el-option label="未执行" :value="1" />
						<el-option label="执行中" :value="2" />
						<el-option label="已完成" :value="3" />
						<el-option label="手动关闭" :value="4" />
						<el-option label="已过期" :value="5" />
					</el-select>
				</el-form-item>
				<el-form-item label="截止时间：" prop="date">
					<date-range v-model.trim="searchForm.date"></date-range>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" plain @click="searchSubmit('searchForm')" class="margin-left">搜索
					</el-button>
					<el-button type="info" plain @click="searchReset">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<el-table stripe tooltip-effect="dark" style="width: 100%" border highlight-current-row row-key="id" size="mini"
			v-loading="tabLoading" :data="tableData" :row-class-name="tableRowClassName" @sort-change="sortColumn"
			@selection-change="handleSelectionChange">
			<el-table-column type="index" label="序号" width="45" align="center">
				<template slot-scope="scope">
					<span>{{
              hasPage ? ((listParams.page - 1) * listParams.page_size + scope.$index + 1) : (scope.$index + 1)
            }}</span>
				</template>
			</el-table-column>
			<el-table-column label="类型" width="70" align="center">
				<template slot-scope="scope">
					<el-tag size="mini" v-if="scope.row.work_order_type_id==1">上刊</el-tag>
					<el-tag size="mini" v-else-if="scope.row.work_order_type_id==2" type="danger">下刊</el-tag>
					<el-tag size="mini" v-else-if="scope.row.work_order_type_id==3" type="warning">巡检</el-tag>
					<el-tag size="mini" v-else-if="scope.row.work_order_type_id==4" type="success">新安装</el-tag>
					<el-tag size="mini" v-else-if="scope.row.work_order_type_id==5" type="info">维修</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="name" label="名称" min-width="150" show-overflow-tooltip></el-table-column>
			<el-table-column prop="deadline" label="截止时间" align="center" width="135" show-overflow-tooltip sortable>
			</el-table-column>
			<el-table-column prop="newest_worker_submit" label="工人最新提交" align="center" width="135" show-overflow-tooltip
				sortable="custom">
			</el-table-column>
			<!-- <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column> -->
			<el-table-column prop="created_at" label="工单创建时间" align="center" width="135" show-overflow-tooltip sortable>
			</el-table-column>
			<el-table-column width="145" align="center" fixed="right">
				<template slot="header" slot-scope="scope">
					<el-tooltip class="item" effect="dark" content="实际设备数量,不包含重复提交" placement="top">
						<span>进度_(<span style="color:orange">待审</span>+通过)/总数</span>
					</el-tooltip>
				</template>
				<template slot-scope="scope">
					(<span :style="{color:(scope.row.installed_to_check_num>0?'orange':'#111')}">{{
            scope.row.installed_to_check_num
          }}</span>+{{ scope.row.installed_passed_num }})
					/ {{ scope.row.total_num }}
				</template>
			</el-table-column>
			<el-table-column label="执行状态" width="85" align="center" fixed="right">
				<template slot-scope="scope">
					<el-tag size="mini" v-if="!scope.row.closed && scope.row.is_exe && !scope.row.is_expire">执行中
					</el-tag>
					<el-tag size="mini" v-else-if="scope.row.closed && scope.row.hand_closed" type="success">手动关闭
					</el-tag>
					<el-tag size="mini" v-else-if="scope.row.closed && !scope.row.hand_closed" type="success">已完成
					</el-tag>
					<el-tag size="mini" v-else-if="!scope.row.closed && scope.row.is_expire" type="warning">已过期
					</el-tag>
					<el-tag size="mini" v-else type="info">未执行</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" :width="getOperateWidth" align="center" fixed="right">
				<template slot-scope="scope">
					<div class="operation">
						<el-button-group>
							<el-button size="mini" icon="el-icon-view" @click.native="progressView(scope.row.id)">进度查看
							</el-button>
							<el-button v-if="hasCheckBTN" icon="el-icon-coordinate" type="warning" size="mini"
								@click.native="check(scope.row)">审核
							</el-button>
							<el-button v-if="hasEditBTN" icon="el-icon-edit-outline" type="primary" size="mini"
								@click.native="edit(scope.row)"
								:disabled="scope.row.closed && scope.row.work_order_type_id!=4">编辑
							</el-button>
							<el-button v-if="hasDelBTN" icon="el-icon-delete" type="danger" size="mini"
								@click.native="del(scope.row)" 
								:disabled="(scope.row.closed || scope.row.is_exe || scope.row.is_expire) && (scope.row.total_num || 0) > 0">
								删除
							</el-button>
							<!-- 							<el-button v-if="hasExportBTN" icon="el-icon-search" size="mini"
								@click.native="exp(scope.row)">
								导出
							</el-button> -->
							<el-button v-if="hasReportBTN && scope.row.report_can_download==null" type="warning"
								icon="el-icon-warning-outline" size="mini" @click.native="showGenDialog(scope.row)">生成报告
							</el-button>
							<el-tooltip class="item" effect="dark" content="报告正在生成中,请稍后刷新页面" placement="bottom">
								<el-button v-if="hasReportBTN && scope.row.report_can_download==0"
									icon="el-icon-loading" size="mini">正在生成
								</el-button>
							</el-tooltip>
							<el-tooltip class="item" effect="dark" content="报告生成成功,可点击进行下载,或重新生成" placement="bottom">
								<el-button v-if="hasReportBTN && scope.row.report_can_download==1" type="success"
									icon="el-icon-download" size="mini" @click.native="showDownload(scope.row);">下载报告
								</el-button>
							</el-tooltip>
							<el-tooltip class="item" effect="dark" content="生成报告错误,请联系管理员" placement="bottom">
								<el-button v-if="hasReportBTN && scope.row.report_can_download==2" type="danger"
									icon="el-icon-error" size="mini">生成出错
								</el-button>
							</el-tooltip>
							<!-- <el-button icon="el-icon-download" size="mini"
								v-if="!scope.row.img_zip_path && !scope.row.img_zip_can_download && scope.row.img_zip_can_download!=0 && !scope.row.click"
								@click.native="downLoadZip(scope.row,listParamsProp.work_order_type_id)">生成图片包
							</el-button>
							<el-tooltip class="item" effect="dark" content="图片包生成成功,可点击进行下载,或重新生成" placement="bottom">
								<el-button v-if="scope.row.img_zip_path && scope.row.img_zip_can_download==1"
									type="success" icon="el-icon-download" size="mini"
									@click.native="showRegenerateDialog(scope.row);">下载图片包
								</el-button>
							</el-tooltip>
							<el-tooltip class="item" effect="dark" content="图片包正在生成中,请稍后刷新页面" placement="bottom">
								<el-button
									v-if="(!scope.row.img_zip_path && scope.row.img_zip_can_download==0) || scope.row.click"
									icon="el-icon-loading" size="mini">正在生成
								</el-button>
							</el-tooltip> -->
							<el-button v-if="hasCloseBTN" icon="el-icon-collection" type="success" size="mini"
								@click.native="close(scope.row)" :disabled="scope.row.closed">关闭
							</el-button>
						</el-button-group>
					</div>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页数据 -->
		<el-pagination v-if="hasPage" @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page.sync="current" :page-sizes="[10, 20, 50, 100, 200, 500]" :page-size="listParams.page_size"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>

		<el-dialog :close-on-click-modal="false" title="生成报告" center :visible.sync="dialogGen" width="80%"
			@close="dialogClose('genForm')">
			<el-form class="padding-bottom" ref="genForm" size="mini" v-loading="dialogGenLoading" :model="genForm"
				:rules="genRules" label-position="right" label-width="120px">
				<el-divider content-position="left">基本配置项</el-divider>
				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="文件名" prop="file_name">
							<el-input clearable v-model.trim="genForm.file_name" placeholder="请输入文件名">
								<template slot="append">.pptx</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="每设备一张图" prop="per_device_one_photo">
							<el-select filterable v-model="genForm.per_device_one_photo" placeholder="请选择"
								style="width:100%">
								<el-option v-for="item in per_device_one_photoList" :key="item.id" :label="item.name"
									:value="item.id">
									<span style="float: left">{{ item.name }}</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="每页照片数量" prop="per_ppt_photo_nums">
							<el-select filterable v-model="genForm.per_ppt_photo_nums" placeholder="请选择"
								style="width:100%">
								<el-option v-for="item in per_ppt_photo_numsList" :key="item.id" :label="item.name"
									:value="item.id">
									<span style="float: left">{{ item.name }}</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider content-position="left">PPT 首页</el-divider>
				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="标题" prop="title">
							<el-input clearable v-model.trim="genForm.title" placeholder="请输入标题" />
						</el-form-item>
						<el-form-item label="类型" prop="type">
							<el-input clearable v-model.trim="genForm.type" placeholder="请输入类型" />
						</el-form-item>
						<el-form-item label="显示客户名" prop="show_customer">
							<el-switch v-model="genForm.show_customer" />
						</el-form-item>
						<el-form-item label="客户名" prop="customer_name" v-if="genForm.show_customer">
							<el-input clearable v-model.trim="genForm.customer_name" placeholder="请输入客户名" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="副标题" prop="sub_title">
							<el-input clearable v-model.trim="genForm.sub_title" placeholder="请输入副标题" />
						</el-form-item>
						<el-form-item label="公司名" prop="company_name">
							<el-input clearable v-model.trim="genForm.company_name" placeholder="请输入公司名" />
						</el-form-item>
						<el-form-item label="显示时间段" prop="show_daterange">
							<el-switch v-model="genForm.show_daterange" />
						</el-form-item>
						<input-group v-if="genForm.show_daterange" :list.sync="genForm.daterange" title="时间段"
							prop="daterange" placeholder="请输入时间段" />
					</el-col>
				</el-row>
				<el-row :gutter="10">
					<el-col :span="20">
						<el-form-item label="画刊原图" prop="company_name">
							<el-upload :action="getImgUploadUrl()" :headers="headers" :file-list="imgList"
								list-type="picture-card" :on-preview="handlePictureCardPreview"
								:on-remove="handleRemove" :on-success="uploadSuccess" :on-error="uploadError"
								:before-upload="beforeUpload" accept="image/*" v-loading="load">
								<i class="el-icon-plus"></i>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click.native="dialogGen=false;dialogClose('genForm')">取 消</el-button>
				<el-button type="primary" @click.native="onGen()">生 成</el-button>
			</span>
		</el-dialog>

		<el-dialog title="下载提示" :visible.sync="confirmDownloadVisible" width="30%"
			@close="dialogClose('confirmDownloadDialog')">
			<span>您确认要下载该报告?或重新生成?</span>
			<span slot="footer" class="dialog-footer">
				<el-button size="mini" @click="confirmDownloadVisible = false;dialogClose('confirmDownloadDialog')">取 消
				</el-button>
				<el-button size="mini" type="warning" @click="showReGenDialog()">重新生成</el-button>
				<el-button size="mini" type="primary" @click="confirmDownLoad()">下 载</el-button>
			</span>
		</el-dialog>
		<el-dialog title="下载提示" :visible.sync="regenerateDialog" width="30%" @close="regenerateDialogClose">
			<span>您确认要下载该图片包?或重新生成?</span>
			<span slot="footer" class="dialog-footer">
				<el-button size="mini" @click="regenerateDialog = false">取 消
				</el-button>
				<el-button size="mini" type="warning" @click="regenerate(tmpRow)">重新生成</el-button>
				<el-button size="mini" type="primary" @click="downloadImgZip(tmpRow);regenerateDialog = false">下 载
				</el-button>
			</span>
		</el-dialog>
		<el-dialog width="30%" :visible.sync="imgDialogVisible" title="图片预览">
			<img width="100%" height="auto" :src="dialogImageUrl" alt="" style="margin-bottom: 20px">
		</el-dialog>

		<el-dialog title="进度详情" :visible.sync="buildingAreaSubmitVisible" @close="buildingAreaSubmitDialogClose"
			width="50%" center>
			<el-form :inline="true" :model="buildingAreaSubmitModel">
				<el-form-item prop="building_name">
					<el-input size="small" v-model="buildingAreaSubmitModel.building_name" placeholder="楼盘名称">
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-button size="small" type="primary" @click.native="getBuildingAreaDataList">查询</el-button>
				</el-form-item>
			</el-form>
			<el-table :data="buildingAreaSubmitData" v-loading="buildingAreaSubmitDataLoading" border>
				<el-table-column property="building_name" label="楼盘名称" width="150"></el-table-column>
				<el-table-column property="all_num" label="总数" width="200"></el-table-column>
				<el-table-column label="提交数">
					<template slot-scope="scope">
						<span>{{ scope.row.submit_num ? scope.row.submit_num : '0' }}</span>
					</template>
				</el-table-column>
				<el-table-column property="not_submit_num" label="未提交数"></el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="primary" @click="look(scope.row.building_area_id,true)">已提交
						</el-button>
						<el-button size="mini" v-if="listParamsProp.work_order_type_id!=4"
							@click="look(scope.row.building_area_id,false)">未提交
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<span slot="footer" class="dialog-footer">
				<el-button @click="buildingAreaSubmitVisible = false">取 消</el-button>
			</span>
			<el-dialog width="40%" :title="(isSubmitted?'已提交':'未提交')+'设备详情'" :visible.sync="deviceDialogVisible"
				append-to-body center @close="deviceDialogClose">
				<el-form :inline="true" :model="deviceListParams">
					<el-form-item prop="building_name">
						<el-input size="small" v-model="deviceListParams.code" placeholder="设备编号"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click.native="getOrderDetail">查询</el-button>
					</el-form-item>
				</el-form>
				<el-table :data="deviceListData" v-loading="deviceListLoading" border>
					<el-table-column property="typeName" label="媒体类型"></el-table-column>
					<el-table-column label="编号">
						<template slot-scope="scope">
							<span>{{ scope.row.code ? scope.row.code : '待审批暂无信息' }}</span>
						</template>
					</el-table-column>
					<el-table-column label="详情">
						<template slot-scope="scope">
							<span>{{ scope.row.detail ? scope.row.detail : '待审批暂无信息' }}</span>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination @size-change="deviceHandleSizeChange" @current-change="deviceHandleCurrentChange"
					:current-page="deviceListParams.page" :page-sizes="[10,20,50,100, 200, 400]"
					:page-size="deviceListParams.page_size" layout="total, sizes, prev, pager, next, jumper"
					:total="deviceTotal">
				</el-pagination>
				<span slot="footer" class="dialog-footer">
					<el-button @click="deviceDialogVisible = false">关闭</el-button>
				</span>
			</el-dialog>
		</el-dialog>
	</div>
</template>

<script>
	/* import {
		del,
		downloadAllDeviceImg,
		genWorkOrderReport,
		getBuildingAreaSubmitInfoByOrderId,
		getOrderDetail,
		getOrderDeviceInfo,
		getWorkOrderReportInfo,
		hand_close,
		isHaveDeviceImg
	} from '@/api/system/workorder.js' */
	import DateRange from '@/components/DateRange/index-old'
	import common from '@/utils/common.js';
	import InputGroup from '@/components/InputGroup';
	import {
		cloneDeep
	} from 'lodash'
	import {
		getUploadUrl
	} from "@/api/upload";
	import {
		getToken
	} from "@/utils/auth";

	export default {
		name: 'WorkOrderPaneContent',
		// beforeRouteLeave(to, from, next) {
		// 	this.$destroy();
		// 	next();
		// },
		beforeDestroy() {},
		components: {
			DateRange,
			InputGroup
		},
		props: {
			hasPage: {
				type: Boolean,
				default: true
			},
			getListRequest: {
				type: Function,
				required: true
			},
			listParamsProp: {
				type: Object,
				default: () => ({})
			},
			hasCheckBTN: {
				type: Boolean,
				default: false
			},
			hasCloseBTN: {
				type: Boolean,
				default: false
			},
			hasEditBTN: {
				type: Boolean,
				default: false
			},
			hasDelBTN: {
				type: Boolean,
				default: false
			},
			hasExportBTN: {
				type: Boolean,
				default: false
			},
			hasReportBTN: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				headers: {},
				confirmDownloadVisible: false,
				pptCurRow: {},
				per_ppt_photo_numsList: [{
					name: "2",
					id: 2
				}, {
					name: "3",
					id: 3
				}, {
					name: "4",
					id: 4
				}, {
					name: "8",
					id: 8
				}],
				per_device_one_photoList: [{
					name: "是",
					id: true
				}, {
					name: "否",
					id: false
				}],
				dialogGenLoading: false,
				dialogGen: false,
				genForm: {
					id: null,
					file_name: null,
					title: null,
					sub_title: null,
					show_customer: true,
					customer_name: null,
					type: null,
					company_name: null,
					show_daterange: true,
					daterange: [''],
					per_device_one_photo: true,
					per_ppt_photo_nums: 3,
					img_origin_list: [],
				},
				genRules: {
					file_name: [{
						required: true,
						message: '请输入文件名',
						trigger: 'blur'
					}],
				},
				genRow: null,

				// search form
				searchForm: {
					name: null,
					date: [],
					start_at: null,
					end_at: null,
					exe_status: null,
					hand_closed: null,
					closed: null,
					is_exe: null,
				},
				// page option
				total: 0, // 总页数
				current: 1, // 当前页
				// list table
				tableData: [], // 列表数据
				selectIds: [], // 多选选项id
				listParams: {
					name: null,
					date: [],
					start_at: null,
					end_at: null,
					exe_status: null,
					hand_closed: null,
					closed: null,
					is_exe: null,
					is_expire: null,
					page: 1,
					page_size: 10
				}, // list参数
				tabLoading: false,
				imgDialogVisible: false,
				dialogImageUrl: null,
				imgList: [],
				load: false,
				regenerateDialog: false,
				tmpRow: null,
				buildingAreaSubmitData: [],
				buildingAreaSubmitVisible: false,
				buildingAreaSubmitDataLoading: false,
				buildingAreaSubmitModel: {
					building_name: null,
				},
				tmpOrderId: null,
				isSubmitted: false,
				tmpBuildingAreaId: null,
				deviceListParams: {
					code: null,
					page: 1,
					page_size: 10
				},
				deviceTotal: null,
				deviceDialogVisible: false,
				deviceListData: [],
				deviceListLoading: false
			}
		},
		computed: {
			getOperateWidth() {
				const that = this
				let count = 3
				if (that.hasCheckBTN) count++
				if (that.hasCloseBTN) count++
				if (that.hasEditBTN) count++
				if (that.hasDelBTN) count++
				if (that.hasExportBTN) count++
				// return (count * 70 + 50 + (that.hasReportBTN ? 93 : 0))
				return (count * 57 + 25 + (that.hasReportBTN ? 81 : 0))
			}
		},
		created() {
			this.getToken();
		},
		mounted() {
			//this.getList()
		},
		methods: {
			downloadImgZip(row) {
				var zipUrl = row.img_zip_path;
				if (!zipUrl) {
					common.messageError('图片不存在，请联系管理员！');
					return false;
				}
				this.downloadFile(zipUrl);
			},
			regenerateDialogClose() {
				this.tmpRow = null;
			},
			regenerate(row) {
				this.downLoadZip(row, row.id, true);
				this.regenerateDialog = false;
			},
			showRegenerateDialog(row) {
				this.tmpRow = row;
				this.regenerateDialog = true;
			},
			handleRemove(file, fileList) {
				console.log(file)
				if (!!file.response)
					this.genForm.img_origin_list = this.genForm.img_origin_list.filter(item => item != file.response.data);
				else
					this.genForm.img_origin_list = this.genForm.img_origin_list.filter(item => item != file.url);
			},
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.imgDialogVisible = true;
			},
			getImgUploadUrl() {
				return getUploadUrl();
			},
			getToken() {
				this.headers = {
					Authorization: getToken()
				}
			},
			beforeUpload() {
				this.load = true;
			},
			uploadSuccess(response, file, fileList) {
				this.load = false;
				this.genForm.img_origin_list.push(response.data);
			},
			uploadError() {
				this.load = false;
			},
			downLoadZip(row, id, flag = false) {
				isHaveDeviceImg({
					workOrderId: row.id,
					work_order_type_id: id
				}).then(
					res => {
						if (res.data > 0) {
							if (flag) {
								downloadAllDeviceImg({
									workOrderId: row.id,
									work_order_type_id: id
								}).then(
									res => {
										this.$message({
											message: res.message,
											type: 'success'
										});
										// this.getList();
										for (let i = 0; i < this.tableData.length; i++) {
											if (this.tableData[i].id == row.id) {
												this.tableData[i].click = true;
												this.tableData[i].img_zip_path = null;
											}
										}
									}
								)
							} else {
								this.$confirm('确定生成设备图片包', '提示', {
									confirmButtonText: '确定',
									cancelButtonText: '取消',
									type: 'warning'
								}).then(() => {
									downloadAllDeviceImg({
										workOrderId: row.id,
										work_order_type_id: id
									}).then(
										res => {
											this.$message({
												message: res.message,
												type: 'success'
											});
											// this.getList();
											for (let i = 0; i < this.tableData.length; i++) {
												if (this.tableData[i].id == row.id) {
													this.tableData[i].click = true;
												}
											}
										}
									)
								}).catch(() => {

								});
							}
						} else {
							this.$message({
								showClose: true,
								message: '此工单暂无设备',
								type: 'warning'
							});
						}
					}
				).catch(() => {

				})
			},
			sortColumn({
				column,
				prop,
				order
			}) {
				if (order === null) {
					delete this.listParams.order_by;
				} else {
					this.listParams.order_by = prop + (order == 'ascending' ? ' ASC' : ' DESC');
				}
				this.getList()
			},
			onGen() {
				const that = this
				if (that.genRow) that.genRow.report_can_download = 0
				genWorkOrderReport(that.genForm)
					.then(({
						message
					}) => {
						common.messageSuccess(message)
						that.resetGenForm()
						that.dialogGen = false
					})
			},
			showGenDialog(row) {
				const that = this
				this.imgList = [];
				this.genForm.img_origin_list = [];
				//获取工单下设备信息
				getOrderDeviceInfo({
					workOrderId: row.id
				}).then(
					res => {
						for (let i = 0; i < res.data.length; i++) {
							this.genForm.img_origin_list.push(res.data[i]);
							this.imgList.push({
								url: res.data[i]
							});

						}
					}
				)
				that.dialogGenLoading = true
				// 查询生成报告基本信息，填充form
				getWorkOrderReportInfo({
						id: row.id
					})
					.then(({
						data
					}) => {
						that.genRow = row
						that.genForm = Object.assign(that.genForm, cloneDeep(data))
						that.dialogGenLoading = false
						that.dialogGen = true
					})
			},
			showReGenDialog() {
				const that = this
				that.pptCurRow = {};
				that.confirmDownloadVisible = false;
				that.showGenDialog(that.pptOriginRow)
			},
			resetGenForm() {
				this.genForm = {
					id: null,
					file_name: null,
					title: null,
					sub_title: null,
					show_customer: true,
					customer_name: null,
					type: null,
					company_name: null,
					show_daterange: true,
					daterange: [''],
					per_device_one_photo: true,
					per_ppt_photo_nums: 3
				}
			},
			dialogClose(formName) {
				const that = this
				switch (formName) {
					case 'genForm':
						that.genForm = {
							id: null,
							file_name: null,
							title: null,
							sub_title: null,
							show_customer: true,
							customer_name: null,
							type: null,
							company_name: null,
							show_daterange: true,
							daterange: [''],
							per_device_one_photo: true,
							per_ppt_photo_nums: 3,
							img_origin_list: [],
						};
						this.imgList = [];
						break;
					case 'confirmDownloadDialog':
						that.pptCurRow = {};
						break;
				}
			},
			downloadFile(url) {
				window.open(url, '_blank')
			},
			confirmDownLoad() {
				const that = this
				const url = that.pptCurRow.report_path
				if (!url) {
					common.messageError('报告不存在，请联系管理员！');
					return false;
				}
				that.downloadFile(url)
			},
			showDownload(row) {
				this.pptOriginRow = row
				this.pptCurRow = cloneDeep(row);
				this.confirmDownloadVisible = true;
			},
			check(row) {
				const that = this
				that.$nextTick(() => {
					that.$emit('check', row, this.listParamsProp.work_order_type_id)
				})
			},
			async close(row) {
				const flag = await common.confirmDialog('您确认要关闭?')
				if (flag) {
					hand_close(row.id).then(res => {
						let {
							code,
							date,
							message
						} = res;
						this.getList();
						common.messageSuccess(message)
					});
				}
			},
			edit(row) {
				this.$router.push({
					name: 'work_order_contract_edit_page',
					params: {
						id: row.id
					}
				})
			},
			async del(row) {
				const flag = await common.confirmDialog('您确认要删除?')
				if (flag) {
					let ids = row.id ? [row.id] : this.selectIds;
					del(ids).then(res => {
						let {
							code,
							date,
							message
						} = res;
						this.getList();
						common.messageSuccess(message)
					});
				}
			},
			exp(row) {
				const routeData = this.$router.resolve({
					name: `work_order_contract_export_page`,
					query: {
						id: row.id
					}
				});
				window.open(routeData.href, '_blank');
			},


			getList() {
				const that = this
				that.tabLoading = true
				that.getListRequest(Object.assign(that.listParams, that.listParamsProp || {}))
					.then(res => {
						let _toCheckCount = 0
						if (that.hasPage) {
							const {
								data: {
									records,
									toCheckCount,
									total
								}
							} = res
							that.tableData = [];
							for (let i = 0; i < records.length; i++) {
								var tmpObj = records[i];
								tmpObj.click = false;
								that.tableData.push(tmpObj);
							}
							// that.tableData = records
							that.total = total || 0
							_toCheckCount = toCheckCount
						} else {
							const {
								data
							} = res
							that.tableData = data
							that.total = data.length || 0
						}
						that.tabLoading = false
						that.emitTotal(_toCheckCount || 0)
					})
			},
			// 重置搜索表单
			searchReset() {
				this.$refs.searchForm.resetFields()
				let params = this.$refs.searchForm.model
				params.start_at = null
				params.end_at = null
				params.exe_status = null
				params.closed = null
				params.is_exe = null
				params.hand_closed = null
				params.is_expire = null
				this.listParams = Object.assign(this.listParams, params)
				this.getList()
			},
			// 搜索
			searchSubmit(formName) {
				this.$refs[formName].validate(valid => {
					if (valid) {
						const params = this.$refs[formName].model
						this.listParams.page = 1
						this.current = 1
						if (params.date[0]) params.start_at = params.date[0]
						if (params.date[1]) params.end_at = params.date[1]
						if (params.exe_status) {
							if (params.exe_status == 1) {
								params.closed = 0
								params.is_exe = 0
							}
							if (params.exe_status == 2) {
								params.closed = 0
								params.is_exe = 1
							}
							if (params.exe_status == 3) {
								params.closed = 1
							}
							if (params.exe_status == 4) {
								params.closed = 1
								params.hand_closed = 1
							}
							if (params.exe_status == 5) {
								params.closed = 0
								params.hand_closed = 0
								params.is_expire = 1
							}
						}
						this.listParams = Object.assign(this.listParams, params)
						this.getList()
					} else {
						console.log('搜索出错!')
						return false
					}
				})
			},
			// 分页 -pageSize改变
			handleSizeChange(val) {
				this.listParams.page_size = val
				this.getList()
			},
			// 分页 -pageNum改变
			handleCurrentChange(val) {
				this.listParams.page = val
				this.getList()
			},
			// 表单多选
			handleSelectionChange(val) {
				this.selectIds = val.map(item => item.id)
			},
			//带状态表格
			tableRowClassName({
				row
			}) {
				return "";
			},


			emitTotal(toCheckTotal) {
				const that = this
				that.$nextTick(() => {
					that.$emit('getTotal', toCheckTotal)
				})
			},
			buildingAreaSubmitDialogClose() {
				this.buildingAreaSubmitData = [];
				this.tmpOrderId = null
				this.buildingAreaSubmitDataLoading = false;
				this.buildingAreaSubmitVisible = false;
				this.buildingAreaSubmitModel.building_name = null
			},
			progressView(id) {
				this.buildingAreaSubmitVisible = true
				this.tmpOrderId = id
				this.getBuildingAreaDataList()
			},
			getBuildingAreaDataList() {
				this.buildingAreaSubmitDataLoading = true;
				getBuildingAreaSubmitInfoByOrderId({
					orderId: this.tmpOrderId,
					building_name: this.buildingAreaSubmitModel.building_name
				}).then(
					res => {
						this.buildingAreaSubmitDataLoading = false;
						this.buildingAreaSubmitData = res.data
					}
				).catch(() => {
					this.buildingAreaSubmitDataLoading = false;
					this.buildingAreaSubmitData = []
				})
			},
			look(id, flag) {
				this.deviceDialogVisible = true
				this.tmpBuildingAreaId = id
				this.isSubmitted = flag
				this.getOrderDetail()
			},
			deviceDialogClose() {
				this.deviceTotal = null;
				this.deviceListData = [];
				this.tmpBuildingAreaId = null;
				this.isSubmitted = false
				this.deviceListParams.code = null
			},
			getOrderDetail() {
				const params = Object.assign({
					orderId: this.tmpOrderId,
					orderType: this.listParamsProp.work_order_type_id,
					isSubmitted: this.isSubmitted,
					building_area_id: this.tmpBuildingAreaId,
				}, this.deviceListParams)
				this.deviceListLoading = true
				getOrderDetail(params).then(
					res => {
						this.deviceListData = res.data.records;
						this.deviceTotal = res.data.total;
						this.deviceListLoading = false
					}
				).catch(() => {
					this.deviceListLoading = false
				})
			},
			deviceHandleSizeChange(val) {
				this.deviceListParams.page_size = val
				this.getOrderDetail()
			},
			deviceHandleCurrentChange(val) {
				this.deviceListParams.page = val
				this.getOrderDetail()
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
